package com.mingle.iwalk;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;

public class PictureViewerActivity extends Activity {

	private POIInfo mInfo;
	private Gallery mPhotoGallery;
	private GalleryAdapter mAdapter;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.picture_viewer_layout);
        
        mPhotoGallery = (Gallery) findViewById(R.id.pic_view);
        
        TravelerDB travelerDB = TravelerDB.getInstance(getContext());
        
        int id = getIntent().getIntExtra("INFO_ID", TravelerInfo.INFO_INVALID_ID);
		if(id != TravelerInfo.INFO_INVALID_ID)
		{ 
			mInfo = (POIInfo) travelerDB.getTravelerInfo(id, TravelerInfo.INFO_TYPE_POI);
			if(mInfo == null || mInfo.picPath == null||mInfo.picPath.length()==0)
			{
				finish();
				return;
			}
			
			List<POIInfo> dataList = TravelerDB.getInstance(getContext()).getTravelerInfoWithPic();
			int position = -1;
			for(int i = 0; i < dataList.size(); i++)
			{
				if(dataList.get(i).id == mInfo.id)
				{
					position = i;
					break;
				}
			}
			
			mAdapter = new GalleryAdapter(getContext(), this, dataList);
			mPhotoGallery.setAdapter(mAdapter);
			mPhotoGallery.setSelection(position==-1?0:position, true);
			mPhotoGallery.setSpacing(5);
			mPhotoGallery.setOnItemClickListener(new OnItemClickListener()
			{

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int pos, long id) {
					
					mAdapter.toggleControlPanel();
				}
				
			});
			
		}
		else
		{
			finish();
			return;
		}
	}
	
	private Context getContext()
	{
		return PictureViewerActivity.this;
	}
    
}
